package com.jnu.book.data;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;

import com.jnu.book.R;

import java.util.Random;

import static com.baidu.vi.VIContext.getContext;

public class CircleSpriter {
    private float x,y,rad;
    double direction;
    float maxWidth,maxHeight;


    public CircleSpriter(float x, float y, float rad, float maxWidth, float maxHeight) {
        this.x = x;
        this.y = y;
        this.rad = rad;
        this.direction=Math.random();
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
    }

    public CircleSpriter(float x, float y, float rad, float direction) {
        this.x = x;
        this.y = y;
        this.rad = rad;
        this.direction = direction;
    }

    public CircleSpriter(float x, float y, float rad) {
        this.x = x;
        this.y = y;
        this.rad = rad;

    }

    public void draw(Canvas canvas){
        Paint paint=new Paint();
        //paint.setColor(Color.RED);
        Bitmap bitmap= BitmapFactory.decodeResource(getContext().getResources(), R.drawable.mouse);
        canvas.drawBitmap(bitmap,x,y,paint);
    }
    public void move(){
        Random r=new Random();
        this.x= (float) (50+200* r.nextInt(4));      //随机生成到某个指定的格子
        this.y= (float) (150+200* r.nextInt(4));
        /*this.x+=20*Math.cos(direction);
        this.y+=20*Math.sin(direction);
        if(this.x<0) this.x+=maxWidth;
        if(this.y<0) this.y+=maxHeight;
        if(this.x>maxWidth) this.x-=maxWidth;
        if(this.y>maxHeight) this.y-=maxHeight;*/
    }

    public boolean isShot(float touchX, float touchY) {
        //计算点击位置到地鼠中心（或格子中心）的距离（由于x、y表示的是地鼠左上角的坐标，所以要x-100(y-100)才表示中心坐标
        //纵坐标还要减去300是因为经调试发现整个view的纵坐标都向下平移了约300个像素
        float distance=(touchX-this.x-100)*(touchX-this.x-100)+(touchY-300-this.y-100)*(touchY-300-this.y-100);
        return (distance<rad*rad);  //通过判断距离是否落在格子范围内判断有没有打到地鼠
    }
}
